﻿using Microsoft.VisualBasic.Devices;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinFormsApp1.Common;
using WinFormsApp1.Common.Dto;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace WinFormsApp1.WangHaoQi.Week02Winfroms
{
    public partial class LiveAnimalsShowHtml : Form
    {
        public LiveAnimalsShowHtml()
        {
            InitializeComponent();
            Show();
            SupplierShow();
        }
        // 公开属性
        /// <summary>
        /// 起始页
        /// </summary>
        public int pageindex = 1;
        /// <summary>
        /// 页容量
        /// </summary>
        public int pagesize = 3;
        /// <summary>
        /// 总条数
        /// </summary>
        public int totalcount = 0;
        /// <summary>
        /// 总页数
        /// </summary>
        public int pagecount = 0;
        /// <summary>
        /// 进场登记时间
        /// </summary>
        public DateTime? LiveAnimalsDatea = null;
        /// <summary>
        /// 供应商/养殖户 外健
        /// </summary>
        public int SupplierId = 0;
        /// <summary>
        /// 进场批次
        /// </summary>
        public string CheckInBatcha = "";
        private async void Show()
        {
            dataGridView1.AutoGenerateColumns = false;
            var data = new LiveAnimalsShowDto
            {
                pageindex = pageindex,
                pagesize = pagesize,
                LiveAnimalsDate = LiveAnimalsDatea,
                SupplierId = SupplierId,
                CheckInBatch = CheckInBatcha,
            };
            var items = await HttpClientHelper.RequestUrl<object, ApiResultFY1<List<LiveAnimals>>>(URLCommon.Url2, "/LiveAnimals/LiveAnimalsShow", HttpClientHelper.MethodType.POST, data);
            this.dataGridView1.DataSource = items.Data.list;
            totalcount = items.Data.totalcount;
            pagecount = items.Data.pagecount;
            //分页文字显示
            label5.Text = Convert.ToString(pagecount);
            label8.Text = Convert.ToString(totalcount);
            label11.Text = Convert.ToString(pageindex);
            label14.Text = Convert.ToString(pagesize);

            //分页  页数的下拉框绑定
            List<object> list = new List<object>();
            for (var i = 1; i <= pagecount; i++)
            {
                list.Add(i);
            }
            comboBox4.DataSource = list;
            this.Controls.Add(comboBox4);
        }
        /// <summary>
        /// 按钮查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiButton2_Click(object sender, EventArgs e)
        {
            if (dateTimePicker1.Text != (DateTime.Now).ToString("YYYY/MM/DD"))
            {
                LiveAnimalsDatea = Convert.ToDateTime(dateTimePicker1.Text);
            }
            MessageBox.Show(LiveAnimalsDatea.ToString());
            if (uiComboBox1.Text != null)
            {
                SupplierId = Convert.ToInt32(uiComboBox1.SelectedValue);
            }
            if (uiTextBox1.Text != null)
            {
                CheckInBatcha = uiTextBox1.Text;
            }
            Show();
        }
        /// <summary>
        /// 首页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiButton5_Click_1(object sender, EventArgs e)
        {
            if (pageindex != 1)
            {
                pageindex = 1;
            }
            Show();
        }
        /// <summary>
        /// 尾页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiButton6_Click_1(object sender, EventArgs e)
        {
            if (pageindex != pagecount)
            {
                pageindex = pagecount;
            }
            Show();
        }
        /// <summary>
        /// 上一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiButton3_Click_1(object sender, EventArgs e)
        {
            if (pageindex > 1)
            {
                pageindex--;
            }
            else
            {
                //弹框提示
                MessageBox.Show("已经第一页了");
            }
            Show();
        }
        /// <summary>
        /// 下一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiButton4_Click_1(object sender, EventArgs e)
        {
            if (pageindex < pagecount)
            {
                pageindex++;
            }
            else
            {
                //弹框提示
                MessageBox.Show("已经最后一页了");
            }
            Show();
        }
        /// <summary>
        /// 供应商下拉框绑定
        /// </summary>
        public async void SupplierShow()
        {
            var items = await HttpClientHelper.RequestUrl<object, ApiResult<List<Suppliers>>>(URLCommon.Url2, "/CheckIn/SupplierShow", HttpClientHelper.MethodType.POST, null);
            uiComboBox1.DataSource = items.Data;
            uiComboBox1.DisplayMember = "SupplierName";
            uiComboBox1.ValueMember = "SupplierId";

            this.Controls.Add(uiComboBox1);
        }
        /// <summary>
        /// 供应商
        /// </summary>
        public class Suppliers
        {
            public int SupplierId { get; set; }
            public string SupplierName { get; set; }
        }

        public class LiveAnimals
        {
            /// <summary>
            /// 活畜称重单Id
            /// </summary>
            public int LiveAnimalsId { get; set; }
            /// <summary>
            /// 日期
            /// </summary>
            public DateTime? LiveAnimalsDate { get; set; }
            /// <summary>
            /// 单据号
            /// </summary>
            public string LiveAnimalsDocument { get; set; }
            /// <summary>
            /// 进场编号
            /// </summary>
            public int CheckInId { get; set; }
            /// <summary>
            /// 供应商
            /// </summary>
            public string SupplierName { get; set; }
            /// <summary>
            /// 车牌号
            /// </summary>
            public string CarNo { get; set; }
            /// <summary>
            /// 供应商编号
            /// </summary>
            public int SupplierId { get; set; }
            /// <summary>
            /// 进场批次
            /// </summary>
            public string CheckInBatch { get; set; }
            /// <summary>
            /// 进场数量
            /// </summary>
            public int ApproachNum { get; set; }
            /// <summary>
            /// 净重
            /// </summary>
            public string LiveAnimalsWeight { get; set; }
            /// <summary>
            /// 均重
            /// </summary>
            public string LiveAnimalsRAverageWeight { get; set; }
        }

    }
}
